package seotools.positions;

import obj.Accum;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ExtramobiMainPage {
    private static final int COLUMNS = 4;

    public static void main(String[] args) throws IOException {
        StringBuilder html = new StringBuilder("<html><body>\n");
        List<Accum> accums = readAccums("data/accum.nokia.txt");
        String name = "Аккумулятор Nokia";
        String url = "http://extramobi.ru/product/accumulator_nokia_";
        html.append("<h2><a href=\"http://extramobi.ru/category/nokia_accumulators/\">").append("Аккумуляторы Nokia").append("</a></h2>\n");
        html.append("<table style=\"font-size:12px\">\n");
        accumsTable(html, accums, name, url);
        html.append("</table>\n</body></html>");
        System.out.println(html);
        PrintStream out = new PrintStream(new FileOutputStream("extramobi-main-accums.txt"));
        out.println(html.toString());
    }

    private static void accumsTable(StringBuilder html, List<Accum> accums, String name, String url) {
        int count = 1;
        for (Accum accum : accums) {
            String[] models = accum.devices;
            String accumName = "Аккумулятор " + accum.name.replaceAll("-", " ");
            String accumUrl = ("Accumulator_"+accum.name.replaceAll("\\s", "_")).toLowerCase();
            if (count == 1 || (count % COLUMNS )+ 1 == 0) {
                html.append("<tr>\n");
            }
            html.append("\t<td style=\"padding:0 10 10 0\"><strong><a href=\"http://extramobi.ru/product/").append(accumUrl).append("\">").append(accumName).append("</a></strong></td>\n");
            if (count % COLUMNS == 0) {
                html.append("</tr>\n");
            }
            count++;
            for (int i = 1; i <= models.length; i++) {
                if (count == 1 || (count % COLUMNS) + 1 == 0) {
                    html.append("<tr>\n");
                }
                html.append("\t<td style=\"padding:0 10 10 0\"><strong><a href=\"").append(url).append(models[i - 1].toLowerCase()).append("\">").append(name).append(" ").append(models[i - 1]).append("</a></strong></td>\n");
                if (count % COLUMNS == 0) {
                    html.append("</tr>\n");
                }
                count++;
            }
        }
        if (html.toString().endsWith("</td>\n")) {
            html.append("</tr>\n");
        }
    }

    public static List<Accum> readAccums(String fileName) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        List<Accum> accumsList = new ArrayList<Accum>();
        String line;
        while ((line = reader.readLine()) != null) {
            Accum accum = parseAccum(line);
            accumsList.add(accum);
        }
        return accumsList;
    }

    private static Accum parseAccum(String line) {
        Accum accum = new Accum();
        String[] fields = line.split(";");
        accum.name = fields[0];
        accum.devices = fields[1].split(",");
        return accum;
    }

}
